package com.service.business.otc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.business.base.dto.LoginUserSession;
import com.service.business.base.service.CoinsPriceService;
import com.service.business.base.service.ConfigService;
import com.service.business.member.dto.MemberStoreChange;
import com.service.business.member.service.MemberCapitalMeasurementService;
import com.service.business.member.service.MemberService;
import com.service.business.member.service.MemberStoreService;
import com.service.business.otc.constants.OtcOrderResultEnum;
import com.service.business.otc.controller.rep.OrderTradeDetail;
import com.service.business.otc.controller.rep.OtcConfigRep;
import com.service.business.otc.controller.rep.OtcOrderDetailRep;
import com.service.business.otc.controller.rep.OtcOrderPageRep;
import com.service.business.otc.controller.req.OrderCreateReq;
import com.service.business.otc.controller.req.OrderTradeReq;
import com.service.business.otc.controller.req.OtcOrderPageReq;
import com.service.business.otc.service.OtcOrderDetailService;
import com.service.business.otc.service.OtcOrderService;
import com.service.business.support.MemberStoreLockUtils;
import com.service.business.support.SessionUtil;
import com.service.core.exception.CommonException;
import com.service.core.rest.request.PageRequest;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.enums.MemberJAType;
import com.service.dao.enums.OtcDetailTypeEnum;
import com.service.dao.enums.OtcStatusEnum;
import com.service.dao.enums.OtcTypeEnum;
import com.service.dao.mapper.OtcOrderMapper;
import com.service.dao.model.*;
import com.service.utility.common.UUIDUtil;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("hmOtcOrderService")
@Slf4j
public class OtcOrderServiceImpl extends ServiceImpl<OtcOrderMapper, OtcOrder> implements OtcOrderService {

    private static String memberNameSuffix = "**";

    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private ConfigService configService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private CoinsPriceService coinsPriceService;
    @Autowired
    private MemberStoreService memberStoreService;
    @Autowired
    private OtcOrderDetailService otcOrderDetailService;
    @Autowired
    private MemberCapitalMeasurementService measurementService;

    @Value("${otcOrderPrefix}")
    private String otcOrderPrefix;

    @Override
    @Transactional
    public Integer creatOtcOrder(OrderCreateReq orderCreateReq, int type,Map<String, SysConfig> configMap) {

        memberService.validSafetyPwd(orderCreateReq.getMemberId(), orderCreateReq.getSafetyPwd());

        OtcOrder newOrder = buildNewOrder(orderCreateReq, type, configMap);
        // 校验单价
        assertPriceChangeLimit(configMap, orderCreateReq.getOtcPrice());
        if (newOrder.getOtcMinBuyNum().compareTo(newOrder.getOtcNum()) > 0) {
            if (type == OtcTypeEnum.BUY.getType()) {
                throw new CommonException("最低售出数量不得低于购买数量", "otc_business_exception_min_buy_num_error");
            } else {
                throw new CommonException("最低购买数量不得低于售出数量", "otc_business_exception_min_buy_num_error_2");
            }
        }

        OtcOrder order = getOne(new QueryWrapper<OtcOrder>().lambda()
                .eq(OtcOrder::getMemberId, orderCreateReq.getMemberId())
                .eq(OtcOrder::getCoinsId, newOrder.getCoinsId())
                .eq(OtcOrder::getOtcType, type)
                .and(wrapper -> wrapper.eq(OtcOrder::getOtcStatus, OtcStatusEnum.TRADING.getStatus())
                        .or().eq(OtcOrder::getOtcStatus, OtcStatusEnum.FOR_SALE.getStatus())).last(" limit 1"));
        if (!ObjectUtils.isEmpty(order)) {
            if (type == OtcTypeEnum.BUY.getType()) {
                throw new CommonException("已有买单还在处于交易中", "otc_business_exception_buy_order_exist");
            } else {
                throw new CommonException("已有卖单还在处于交易中", "otc_business_exception_sell_order_exist");
            }
        }

        Integer coinId;
        BigDecimal otcNum;
        if (type == OtcTypeEnum.SELL.getType()) {
            coinId = newOrder.getCoinsId();
            otcNum = newOrder.getOtcNum();
        } else {
            coinId = newOrder.getTradeCoinsId();
            otcNum = newOrder.getOtcNum().multiply(newOrder.getOtcPrice());
        }
        //校验钱包余额
        assertMemberStore(newOrder.getMemberId(), coinId, otcNum);
        save(newOrder);
        processStoreNum(newOrder.getMemberId(), coinId, otcNum, MemberJAType.FROST_OCT, newOrder.getId(), null, null);
        return newOrder.getId();
    }

    private OtcOrder buildNewOrder(OrderCreateReq orderCreateReq, int type, Map<String, SysConfig> configMap) {
        OtcOrder newOrder = new OtcOrder();

        newOrder.setOtcNo(buildOTCOrderNo());
        newOrder.setMemberId(orderCreateReq.getMemberId());

        SysConfig config = configMap.get(ConfigConstants.OTC_COIN_TYPE_ID);
        newOrder.setCoinsId(Integer.parseInt(config.getConfValue()));
        newOrder.setCoinsName(config.getConfOptions());

        config = configMap.get(ConfigConstants.OTC_TRADE_COIN_TYPE_ID);
        newOrder.setTradeCoinsId(Integer.parseInt(config.getConfValue()));
        newOrder.setTradeCoinsName(config.getConfOptions());

        newOrder.setOtcPrice(orderCreateReq.getOtcPrice());
        newOrder.setOtcNum(orderCreateReq.getOtcNum());
        newOrder.setOtcResidueNum(orderCreateReq.getOtcNum());
        newOrder.setOtcMinBuyNum(orderCreateReq.getOtcMinBuyNum());
        newOrder.setOtcType(type);
        newOrder.setOtcStatus(OtcStatusEnum.FOR_SALE.getStatus());
        newOrder.setCreateTime(new Date());

        return newOrder;
    }

    private void assertPriceChangeLimit(Map<String, SysConfig> configMap, BigDecimal otcPrice) {
        BigDecimal priceChangeLimit = BigDecimal.valueOf(Long.parseLong(configMap.get(ConfigConstants.OTC_PRICE_HIGHS_AND_LOWS_LIMIT).getConfValue()));
        CoinsPrice coinsPrice = coinsPriceService.getLastCoinPriceByCoinId(Integer.parseInt(configMap.get(ConfigConstants.OTC_COIN_TYPE_ID).getConfValue()));
        BigDecimal rate = (otcPrice.subtract(coinsPrice.getStandardPrice())).divide(coinsPrice.getStandardPrice(), 4).multiply(BigDecimal.valueOf(100)).abs();
        if (rate.compareTo(priceChangeLimit) > 0) {
            throw new CommonException("单价超出正常涨跌幅", "otc_business_exception_price_change_limit");
        }
    }

    private void assertMemberStore(Integer memberId, Integer coinsId, BigDecimal otcNum) {
        MemberStore memberStore = getMemberStore(memberId, coinsId);
        if (otcNum.compareTo(memberStore.getNum()) > 0) {
            throw new CommonException(OtcOrderResultEnum.STORE_NOT_ENOUGH.getDesc(), "otc_business_exception_store_not_enough");
        }
    }

    private String buildOTCOrderNo() {
        String no = otcOrderPrefix + DateFormatUtils.format(new Date(), "yyyyMMdd") + UUIDUtil.randomUUID10().substring(0, 8);
        OtcOrder otcOrder = getOne(new QueryWrapper<OtcOrder>()
                .lambda().eq(OtcOrder::getOtcNo, no));
        if (!ObjectUtils.isEmpty(otcOrder)) {
            no = buildOTCOrderNo();
        }
        return no;
    }


    @Override
    @Transactional
    public void tradingOtcOrder(OrderTradeReq orderTradeReq, OtcDetailTypeEnum typeEnum,LoginUserSession userSession) {
        memberService.validSafetyPwd(userSession.getUserId(), orderTradeReq.getPayPwd());

        OtcOrder order = getOrderByOrderNo(orderTradeReq.getOtcNo());
        validOtcOrder(order, typeEnum);

        Integer checkCoinId;
        BigDecimal validNum;
        BigDecimal payAmount = orderTradeReq.getBuyNum().multiply(order.getOtcPrice());
        if (ObjectUtils.nullSafeEquals(typeEnum, OtcDetailTypeEnum.SELL)) {
            validNum = orderTradeReq.getBuyNum();
            checkCoinId = order.getCoinsId();
        } else {
            validNum = payAmount;
            checkCoinId = order.getTradeCoinsId();
        }
        tradeValid(order, orderTradeReq, userSession.getUserId(), validNum, typeEnum);

        measurementService.checkJA(userSession.getUserId(), checkCoinId);

        OtcOrderDetail otcOrderDetail = createDetail(orderTradeReq, order, userSession.getUserId(), typeEnum.getType());
        if (ObjectUtils.nullSafeEquals(typeEnum, OtcDetailTypeEnum.SELL)) {

            //买家冻结的钱解冻
            Integer jaId = processStoreNum(order.getMemberId(), order.getTradeCoinsId(), payAmount, MemberJAType.FROST_OCT_RELEASE, order.getId(), otcOrderDetail.getId(), null);
            //卖家交易货币入账--钱包的钱
            processStoreNum(userSession.getUserId(), order.getTradeCoinsId(), payAmount, MemberJAType.OTC_TRADE_INCOME, order.getId(), otcOrderDetail.getId(), jaId);

            //卖家卖出货币出账-----钱包的钱
            jaId = processStoreNum(userSession.getUserId(), order.getCoinsId(), orderTradeReq.getBuyNum(), MemberJAType.OTC_TRADE_PAY, order.getId(), otcOrderDetail.getId(), null);
            //买家买入货币入账-----钱包的钱
            processStoreNum(order.getMemberId(), order.getCoinsId(), orderTradeReq.getBuyNum(), MemberJAType.OTC_TRADE_INCOME, order.getId(), otcOrderDetail.getId(), jaId);

        } else {

            //买家OTC货币出账-----钱包的钱
            Integer jaId = processStoreNum(userSession.getUserId(), order.getTradeCoinsId(), payAmount, MemberJAType.OTC_TRADE_PAY, order.getId(), otcOrderDetail.getId(), null);
            //卖家OTC货币入账-----钱包的钱
            processStoreNum(order.getMemberId(), order.getTradeCoinsId(), payAmount, MemberJAType.OTC_TRADE_INCOME, order.getId(), otcOrderDetail.getId(), jaId);

            //卖家冻结的货币解冻
            jaId = processStoreNum(order.getMemberId(), order.getCoinsId(), orderTradeReq.getBuyNum(), MemberJAType.FROST_OCT_RELEASE, order.getId(), otcOrderDetail.getId(), null);
            //买家买的货币入账--钱包的钱
            processStoreNum(userSession.getUserId(), order.getCoinsId(), orderTradeReq.getBuyNum(), MemberJAType.OTC_TRADE_INCOME, order.getId(), otcOrderDetail.getId(), jaId);
        }

    }

    private void validOtcOrder(OtcOrder otcOrder, OtcDetailTypeEnum typeEnum) {


        if (otcOrder.getOtcStatus() == OtcStatusEnum.COMPLETE.getStatus()) {
            log.info("OTC订单<otcNo:{}>交易已完成", otcOrder.getOtcNo());
            throw new CommonException("OTC交易已完成", "otc_business_exception_complete");
        }

        if (otcOrder.getOtcStatus() == OtcStatusEnum.CANCEL.getStatus()) {
            log.info("OTC订单<otcNo:{}>交易已取消", otcOrder.getOtcNo());
            throw new CommonException("OTC交易已取消", "otc_business_exception_cancel");
        }

        if (ObjectUtils.nullSafeEquals(OtcTypeEnum.SELL.getType(), otcOrder.getOtcType())
                && !ObjectUtils.nullSafeEquals(OtcDetailTypeEnum.BUY, typeEnum)) {
            log.info("OTC订单<orderNo:{}>只允许买入", otcOrder.getOtcNo());
            throw new CommonException("此OTC交易只允许买入操作", "otc_business_exception_only_buy");
        }
        if (ObjectUtils.nullSafeEquals(OtcTypeEnum.BUY.getType(), otcOrder.getOtcType())
                && !ObjectUtils.nullSafeEquals(OtcDetailTypeEnum.SELL, typeEnum)) {
            log.info("OTC订单<orderNo:{}>只允许售出操作", otcOrder.getOtcNo());
            throw new CommonException("此OTC交易只允许售出操作", "otc_business_exception_only_sell");
        }

    }

    /**
     * 交易校验
     */
    private MemberStore tradeValid(OtcOrder order, OrderTradeReq orderTradeReq,
                                   Integer memberId, BigDecimal tradeNum, OtcDetailTypeEnum typeEnum) {

        if (order.getOtcResidueNum().compareTo(order.getOtcMinBuyNum()) >= 0
                && order.getOtcMinBuyNum().compareTo(orderTradeReq.getBuyNum()) > 0) {
            if (ObjectUtils.nullSafeEquals(typeEnum, OtcDetailTypeEnum.BUY)) {
                throw new CommonException("购买数量低于起购数量", "otc_business_exception_min_buy_num");
            } else {
                throw new CommonException("出售数量低于起售数量", "otc_business_exception_min_sell_num");
            }
        } else if (order.getOtcResidueNum().compareTo(orderTradeReq.getBuyNum()) < 0) {
            if (ObjectUtils.nullSafeEquals(typeEnum, OtcDetailTypeEnum.BUY)) {
                throw new CommonException("购买数量超出剩余数量", "otc_business_exception_residue_num_buy");
            } else {
                throw new CommonException("出售数量超出剩余数量", "otc_business_exception_residue_num_sell");
            }
        }
        Integer validCoinId = ObjectUtils.nullSafeEquals(typeEnum, OtcDetailTypeEnum.SELL) ? order.getCoinsId() : order.getTradeCoinsId();
        MemberStore memberStore = getMemberStore(memberId, validCoinId);
        if (memberStore.getNum().compareTo(tradeNum) < 0) {
            throw new CommonException("会员余额不足", "otc_business_exception_store_num_buy");
        }
        return memberStore;
    }

    /**
     * 获取账户
     *
     * @param memberId
     * @param coinTypeId
     * @return
     */
    private MemberStore getMemberStore(Integer memberId, Integer coinTypeId) {
        return memberStoreService.getOne(new QueryWrapper<MemberStore>().lambda()
                .eq(MemberStore::getMemberId, memberId)
                .eq(MemberStore::getCoinsId, coinTypeId));
    }

    @Override
    public OtcOrder getOrderByOrderNo(String orderNo) {
        OtcOrder order = getOne(new QueryWrapper<OtcOrder>().lambda().eq(OtcOrder::getOtcNo, orderNo));
        if (ObjectUtils.isEmpty(order)) {
            log.info("OTC订单<orderNo:{}>不存在", order.getOtcNo());
            throw new CommonException("不存在此交易", "otc_business_exception_not_exist");
        }
        return order;
    }

    /**
     * 创建订单详情 更新订单
     *
     * @param orderTradeReq
     * @param order
     * @param type
     */

    @Transactional
    public OtcOrderDetail createDetail(OrderTradeReq orderTradeReq, OtcOrder order, Integer memberId, int type) {

        BigDecimal payAmount = orderTradeReq.getBuyNum().multiply(order.getOtcPrice());
        OtcOrderDetail otcOrderDetail = ModelMapperUtil.getStrictModelMapper().map(orderTradeReq, OtcOrderDetail.class);
        otcOrderDetail.setOtcOrderNo(order.getOtcNo());
        otcOrderDetail.setOtcOrderId(order.getId());
        otcOrderDetail.setSourceMemberId(order.getMemberId());
        otcOrderDetail.setMemberId(memberId);
        otcOrderDetail.setDetailType(type);
        otcOrderDetail.setCoinsId(order.getCoinsId());
        otcOrderDetail.setCoinsName(order.getCoinsName());
        otcOrderDetail.setOtcPrice(order.getOtcPrice());
        otcOrderDetail.setPayMoney(payAmount);
        otcOrderDetail.setOtcCreateTime(order.getCreateTime());
        otcOrderDetail.setTradeCoinsId(order.getTradeCoinsId());
        otcOrderDetail.setTradeCoinsName(order.getTradeCoinsName());
        otcOrderDetailService.save(otcOrderDetail);

        order.setOtcResidueNum(order.getOtcResidueNum().subtract(orderTradeReq.getBuyNum()));
        if (order.getOtcStatus() == OtcStatusEnum.FOR_SALE.getStatus()) {
            order.setOtcStatus(OtcStatusEnum.TRADING.getStatus());
        }
        if (order.getOtcResidueNum().compareTo(BigDecimal.ZERO) <= 0) {
            order.setOtcStatus(OtcStatusEnum.COMPLETE.getStatus());
        }
        updateById(order);
        return otcOrderDetail;
    }



    @Override
    public OtcOrderDetailRep getDetail(String orderNo) {
        OtcOrder otcOrder = getOne(new QueryWrapper<OtcOrder>().lambda().eq(OtcOrder::getOtcNo, orderNo));
        if (ObjectUtils.isEmpty(otcOrder)) {
            log.error("OTC订单<orderNo:{}>获取详情失败", orderNo);
            throw new CommonException("OTC业务异常:不存在此交易", "otc_business_exception_not_exist");
        }
        OtcOrderDetailRep otcOrderDetailRep = ModelMapperUtil.getStrictModelMapper().map(otcOrder, OtcOrderDetailRep.class);
        buildMemberInfo(otcOrder.getMemberId(), otcOrderDetailRep);

        List<OtcOrderDetail> detailList = otcOrderDetailService.list(new QueryWrapper<OtcOrderDetail>().lambda()
                .eq(OtcOrderDetail::getOtcOrderId, otcOrder.getId()));
        List<OrderTradeDetail> orderTradeDetails = detailList.stream().map(item -> {
            OrderTradeDetail orderTradeDetail = ModelMapperUtil.getStrictModelMapper().map(item, OrderTradeDetail.class);
            String memberName = memberService.getById(item.getMemberId()).getUserName();
            if (StringUtils.isNoneBlank(memberName)) {
                orderTradeDetail.setMemberName(memberName.substring(0, 1) + memberNameSuffix);
            }
            return orderTradeDetail;
        }).collect(Collectors.toList());
        otcOrderDetailRep.setOrderTradeDetailList(orderTradeDetails);
        return otcOrderDetailRep;
    }

    @Override
    public CommonPageRep<OtcOrderPageRep> pageOrder(Integer otcType, PageRequest pageRequest) {

        LambdaQueryWrapper<OtcOrder> queryWrapper = new QueryWrapper<OtcOrder>().lambda().eq(OtcOrder::getOtcType, otcType)
                .in(OtcOrder::getOtcStatus, OtcStatusEnum.getTradingStatus())
                .ne(OtcOrder::getMemberId, sessionUtil.getUser().getUserId());

        if(isDirectRelativeOtcTrade()){
            Member member = memberService.getById(sessionUtil.getUser().getUserId());
            List<Integer> memberList = memberService.selectByPathAndStatus(member.getId()).stream().map(Member::getId).collect(Collectors.toList());
            List<Integer> memberIdList = memberService.getMemberPathList(member.getUserPath());
            memberIdList.addAll(memberList);
            queryWrapper.in(OtcOrder::getMemberId, memberIdList);
        }

        if (ObjectUtils.nullSafeEquals(otcType, OtcTypeEnum.SELL.getType())) {
            queryWrapper.orderByAsc(OtcOrder::getOtcPrice);
        }

        if (ObjectUtils.nullSafeEquals(otcType, OtcTypeEnum.BUY.getType())) {
            queryWrapper.orderByDesc(OtcOrder::getOtcPrice);
        }

        Page<OtcOrder> page = PageHelper.startPage(pageRequest.getPageNo(), pageRequest.getPageSize());
        list(queryWrapper);
        List<OtcOrderPageRep> list = page.stream().map(item -> {
            OtcOrderPageRep otcOrderPageRep = ModelMapperUtil.getStrictModelMapper().map(item, OtcOrderPageRep.class);
            this.buildMemberInfo(item.getMemberId(), otcOrderPageRep);
            return otcOrderPageRep;
        }).collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, list);
    }

    private boolean isDirectRelativeOtcTrade(){
        return configService.configValueIsValid(ConfigConstants.DIRECT_RELATIVE_OTC_TRADE);
    }

    private void buildMemberInfo(Integer memberId, OtcOrderPageRep otcOrderPageRep) {
        Member member = memberService.getById(memberId);
        otcOrderPageRep.setMemberName(member.getUserName());
        otcOrderPageRep.setMemberHeadImg(member.getImgHead());
        otcOrderPageRep.setDealOrderNum(otcOrderDetailService.countDealOrderNum(memberId));
    }


    @Override
    public CommonPageRep<OtcOrderPageRep> pageUserOrder(OtcOrderPageReq pageRequest) {
        Page<OtcOrder> page = PageHelper.startPage(pageRequest.getPageNo(), pageRequest.getPageSize());
        list(new QueryWrapper<OtcOrder>().lambda()
                .eq(OtcOrder::getOtcType, pageRequest.getOtcType())
                .eq(OtcOrder::getMemberId, pageRequest.getMemberId())
                .in(OtcOrder::getOtcStatus, pageRequest.getOtcStatus())
                .orderByDesc(OtcOrder::getUpdateTime));
        List<OtcOrderPageRep> list = page.stream().map(item -> ModelMapperUtil.getStrictModelMapper().map(item, OtcOrderPageRep.class)).collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, list);
    }

    @Override
    @Transactional
    public void cancelOrder(String orderNo) {
        LoginUserSession session = sessionUtil.getUser();
        OtcOrder order = getOne(new QueryWrapper<OtcOrder>().lambda()
                .eq(OtcOrder::getOtcNo, orderNo)
                .eq(OtcOrder::getMemberId, session.getUserId())
                .ne(OtcOrder::getOtcStatus, OtcStatusEnum.COMPLETE.getStatus())
                .ne(OtcOrder::getOtcStatus, OtcStatusEnum.CANCEL.getStatus()));

        if (ObjectUtils.isEmpty(order)) {
            log.error("OTC订单<orderNo:{}>取消订单失败:不存在此交易", orderNo);
            throw new CommonException("OTC业务异常:不存在此交易", "otc_business_exception_not_exist");
        }

        Integer coinTypeId = order.getCoinsId();
        BigDecimal frostNum = order.getOtcResidueNum();
        if (order.getOtcType() == OtcTypeEnum.BUY.getType()) {
            coinTypeId = order.getTradeCoinsId();
            frostNum = order.getOtcResidueNum().multiply(order.getOtcPrice());
        }
        Integer orderId = order.getId();
        Integer memberId = order.getMemberId();
        order = new OtcOrder();
        order.setId(orderId);
        order.setOtcStatus(OtcStatusEnum.CANCEL.getStatus());
        updateById(order);
        processStoreNum(memberId, coinTypeId, frostNum, MemberJAType.OTC_TRADE_CANCEL, orderId, null, null);
    }

    @Transactional
    public Integer processStoreNum(Integer memberId, Integer coinTypeId, BigDecimal tradeAmount, MemberJAType jaType,
                                   Integer orderId, Integer orderDetailId, Integer jaId) {
        MemberStore memberStore = getMemberStore(memberId, coinTypeId);
        MemberStoreChange memberStoreChange = MemberStoreChange.builder()
                .memberId(memberId)
                .coinsId(coinTypeId)
                .coinsName(memberStore.getCoinsName())
                .changeNum(tradeAmount)
                .jaType(jaType)
                .jaId(jaId)
                .remark(jaType.getMsg())
                .orderId(orderId)
                .orderDetailId(orderDetailId)
                .build();

        return memberStoreService.changeMemberStore(memberStore, memberStoreChange);
    }

    @Override
    public OtcConfigRep getOtcConfig() {

//        Map<String, SysConfig> configList = configService.getConfigMapByGroup(ConfigConstants.Grou.OTC.getCode());

        OtcConfigRep.OtcConfigRepBuilder builder = OtcConfigRep.builder();
//        SysConfig config = configList.get(ConfigConstants.OTC_PRICE_HIGHS_AND_LOWS_LIMIT);
//        if (!ObjectUtils.isEmpty(config)) {
//            builder.priceChangeLimit(BigDecimal.valueOf(Long.parseLong(config.getConfValue())));
//        }

//        config = configList.get(ConfigConstants.OTC_COIN_TYPE_ID);
//        builder.coinName(config.getConfOptions());
//        CoinsPrice coinsPrice = coinsPriceService.getLastCoinPriceByCoinId(Integer.parseInt(config.getConfValue()));
//        if (!ObjectUtils.isEmpty(coinsPrice)) {
//            builder.basePrice(coinsPrice.getStandardPrice());
//        }

//        builder.tradeCoinName(configList.get(ConfigConstants.OTC_TRADE_COIN_TYPE_ID).getConfOptions());
//        builder.minBuyNum(BigDecimal.valueOf(Double.parseDouble(configList.get(ConfigConstants.OTC_MIN_BUY_NUM).getConfValue())));

        return builder.build();
    }
}
